<template>
  <div class='math-study'>
    <a-tabs default-active-key="1" @change="callback">
      <a-tab-pane key="1" tab="插入排序">
        <div>
          <textarea cols="50" rows="16" class="textArea">
            //插入排序
            inArr: [4, 3, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2]
            inSort (arr) {
              for (let i = 1; i < arr.length; i++) {
                const key = arr[i]
                let j = i - 1
                while (i >= 0 && arr[i] < arr[j]) {
                  arr[i] = arr[j]
                  arr[j] = key
                  j--
                  i = j
                }
              }
              return arr
            },
          </textarea>
        </div>
      </a-tab-pane>
      <a-tab-pane key="2" tab="归并排序" force-render>
        <div>
          <textarea  cols="50" rows="34" class="textArea">
            // 归并排序
            mergeArr: [6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2]
            AB (arr) {
              const length = arr.length
              if (length < 2) {
                return arr
              }
              const middleIndex = Math.floor(length / 2)
              const leftArr = arr.slice(0, middleIndex)
              const rightArr = arr.slice(middleIndex)
              const left = this.AB(leftArr)
              const right = this.AB(rightArr)
              return this.mergeSort(left, right)
            },
            // 归并排序
            mergeSort (left, right) {
              console.log(left, right)
              const res = []
              while (left.length && right.length) {
                if (left[0] < right[0]) {
                  res.push(left.shift())
                } else {
                  res.push(right.shift())
                }
              }
              while (left.length) {
                res.push(left.shift())
              }
              while (right.length) {
                res.push(right.shift())
              }
              return res
            }
          </textarea>
        </div>
      </a-tab-pane>
    </a-tabs>
  </div>
</template>

<script>
export default {
  name: 'MathStudy',
  props: {

  },
  components: {

  },
  data () {
    return {
      inArr: [4, 3, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2],
      mergeArr: [6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2]
    }
  },
  created () {
    console.log(this.inSort(this.inArr))
    console.log(this.AB(this.mergeArr))
  },
  mounted () {

  },
  watch: {

  },
  methods: {
    callback (key) {
      console.log(key)
    },
    // 插入排序
    inSort (arr) {
      for (let i = 1; i < arr.length; i++) {
        const key = arr[i]
        let j = i - 1
        while (i >= 0 && arr[i] < arr[j]) {
          arr[i] = arr[j]
          arr[j] = key
          j--
          i = j
        }
      }
      return arr
    },
    AB (arr) {
      const length = arr.length
      if (length < 2) {
        return arr
      }
      const middleIndex = Math.floor(length / 2)
      const leftArr = arr.slice(0, middleIndex)
      const rightArr = arr.slice(middleIndex)
      const left = this.AB(leftArr)
      const right = this.AB(rightArr)
      return this.mergeSort(left, right)
    },
    // 归并排序
    mergeSort (left, right) {
      console.log(left, right)
      const res = []
      while (left.length && right.length) {
        if (left[0] < right[0]) {
          res.push(left.shift())
        } else {
          res.push(right.shift())
        }
      }
      while (left.length) {
        res.push(left.shift())
      }
      while (right.length) {
        res.push(right.shift())
      }
      return res
    }
  }
}
</script>

<style lang="scss" scoped>
.math-study{
  .textArea{
    outline: none;
    resize: none;
  }
}
</style>
